package com.oocl.admin_platform.servlet;

import java.io.File;
import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.oocl.admin_platform.pojo.Merchant;
import com.oocl.admin_platform.service.MerchantService;
import com.oocl.admin_platform.service.factory.ServiceFactory;
import com.oocl.admin_platform.util.JsonUtil;

/**
 * Servlet implementation class ListenMerchantUpdateInfoServlet
 */
public class ListenMerchantUpdateInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantService merchantService;
	private static Logger logger = Logger.getLogger(ListenMerchantUpdateInfoServlet.class);
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListenMerchantUpdateInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public void init() throws ServletException {
    	merchantService = ServiceFactory.getMerchantServiceInstance();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idString = request.getParameter("id");
		String timeString = request.getParameter("time");
		Merchant merchant= null;
		String imageTypeString = (String) request.getServletContext().getAttribute("imageType");
		merchant = merchantService.findById(idString);
		String pathString = request.getServletContext().getAttribute("path") + "/"+ request.getServletContext().getAttribute("portraitSrc")+"/"+merchant.getUname()+"2."+imageTypeString;
		File file = new File(pathString);
		long time = file.lastModified();
		String t = String.valueOf(time);
		System.out.println("into========");
		if(!t.equals(timeString)){
			
			String json = JsonUtil.stringToString(t);
			Writer writer = response.getWriter();
			writer.write(json);
		}
	}

}
